// @ts-nocheck
import router from '@system.router';
import BatteryModel from '../../model/aboutBatteryImpl/BatteryModel';
import CommonEvent from '@ohos.commonEvent';
import prompt from '@ohos.prompt';
import Utils from '../../model/Utils'
import batteryInfo from '@ohos.batteryInfo';

let batteryListModel = new BatteryModel;
const LIST_JSON = {
batterySOC: {
    "settingIcon": "/res/image/icon_batterySOC.png",
    "settingType": 0,
    "settingAlias": "batterySOC",
    "settingTitle": "",
    "settingValue": "",
    "settingArrow": "/res/image/ic_settings_arrow.png",
    "settingShowDivider": true,
},
chargingStatus: {
    "settingIcon": "/res/image/icon_chargingStatus.png",
    "settingType": 0,
    "settingAlias": "chargingStatus",
    "settingTitle": "",
    "settingValue": "",
    "settingArrow": "/res/image/ic_settings_arrow.png",
    "settingShowDivider": true,
},
healthStatus: {
    "settingIcon": "/res/image/icon_healthStatus.png",
    "settingType": 0,
    "settingAlias": "healthStatus",
    "settingTitle": "",
    "settingValue": "",
    "settingArrow": "/res/image/ic_settings_arrow.png",
    "settingShowDivider": true,
},
pluggedType: {
    "settingIcon": "/res/image/icon_pluggedType.png",
    "settingType": 0,
    "settingAlias": "pluggedType",
    "settingTitle": "",
    "settingValue": "",
    "settingArrow": "/res/image/ic_settings_arrow.png",
    "settingShowDivider": true,
},
voltage: {
    "settingIcon": "/res/image/icon_voltage.png",
    "settingType": 0,
    "settingAlias": "voltage",
    "settingTitle": "",
    "settingValue": "",
    "settingArrow": "/res/image/ic_settings_arrow.png",
    "settingShowDivider": true,
},
technology: {
    "settingIcon": "/res/image/icon_technology.png",
    "settingType": 0,
    "settingAlias": "technology",
    "settingTitle": "",
    "settingValue": "",
    "settingArrow": "/res/image/ic_settings_arrow.png",
    "settingShowDivider": true,
},
batteryTemperature: {
    "settingIcon": "/res/image/icon_batteryTemperature.png",
    "settingType": 0,
    "settingAlias": "batteryTemperature",
    "settingTitle": "",
    "settingValue": "",
    "settingArrow": "/res/image/ic_settings_arrow.png",
    "settingShowDivider": true,
},
batteryCapacityLevel: {
    "settingIcon": "/res/image/icon_batterySOC.png",
    "settingType": 0,
    "settingAlias": "batteryCapacityLevel",
    "settingTitle": "",
    "settingValue": "",
    "settingArrow": "/res/image/ic_settings_arrow.png",
    "settingShowDivider": true,
},
estimatedRemainingChargeTime: {
    "settingIcon": "/res/image/icon_batterySOC.png",
    "settingType": 0,
    "settingAlias": "estimatedRemainingChargeTime",
    "settingTitle": "",
    "settingValue": "",
    "settingArrow": "/res/image/ic_settings_arrow.png",
    "settingShowDivider": true,
},
totalEnergy: {
    "settingIcon": "/res/image/icon_batterySOC.png",
    "settingType": 0,
    "settingAlias": "totalEnergy",
    "settingTitle": "",
    "settingValue": "",
    "settingArrow": "/res/image/ic_settings_arrow.png",
    "settingShowDivider": true,
},
nowCurrent: {
    "settingIcon": "/res/image/icon_batterySOC.png",
    "settingType": 0,
    "settingAlias": "nowCurrent",
    "settingTitle": "",
    "settingValue": "",
    "settingArrow": "/res/image/ic_settings_arrow.png",
    "settingShowDivider": true,
},
remainingEnergy: {
    "settingIcon": "/res/image/icon_batterySOC.png",
    "settingType": 0,
    "settingAlias": "remainingEnergy",
    "settingTitle": "",
    "settingValue": "",
    "settingArrow": "/res/image/ic_settings_arrow.png",
    "settingShowDivider": true,
},
charging: {
    "settingIcon": "/res/image/icon_chargingStatus.png",
    "settingType": 0,
    "settingAlias": "charging",
    "settingTitle": "",
    "settingValue": "",
    "settingArrow": "/res/image/ic_settings_arrow.png",
    "settingShowDivider": true,
},
level: {
    "settingIcon": "/res/image/icon_batterySOC.png",
    "settingType": 0,
    "settingAlias": "level",
    "settingTitle": "",
    "settingValue": "",
    "settingArrow": "/res/image/ic_settings_arrow.png",
    "settingShowDivider": true,
},
usual_event_BATTERY_CHANGED: {
    "settingIcon": "/res/image/icon_batterySOC.png",
    "settingType": 1,
    "settingAlias": "usual_event_BATTERY_CHANGED",
    "settingTitle": "",
    "settingValue": "",
    "settingArrow": "/res/image/ic_settings_arrow.png",
    "settingShowDivider": true,
    "settingUri": "pages/eventPage/eventPage"
},
usual_event_BATTERY_LOW: {
    "settingIcon": "/res/image/icon_batterySOC.png",
    "settingType": 0,
    "settingAlias": "usual_event_BATTERY_LOW",
    "settingTitle": "",
    "settingValue": "",
    "settingArrow": "/res/image/ic_settings_arrow.png",
    "settingShowDivider": true,
},
usual_event_BATTERY_OKAY: {
    "settingIcon": "/res/image/icon_batterySOC.png",
    "settingType": 0,
    "settingAlias": "usual_event_BATTERY_OKAY",
    "settingTitle": "",
    "settingValue": "",
    "settingArrow": "/res/image/ic_settings_arrow.png",
    "settingShowDivider": true,
},
usual_event_POWER_CONNECTED: {
    "settingIcon": "/res/image/icon_batterySOC.png",
    "settingType": 0,
    "settingAlias": "usual_event_POWER_CONNECTED",
    "settingTitle": "",
    "settingValue": "",
    "settingArrow": "/res/image/ic_settings_arrow.png",
    "settingShowDivider": true,
},
usual_event_POWER_DISCONNECTED: {
    "settingIcon": "/res/image/icon_batterySOC.png",
    "settingType": 0,
    "settingAlias": "usual_event_POWER_DISCONNECTED",
    "settingTitle": "",
    "settingValue": "",
    "settingArrow": "/res/image/ic_settings_arrow.png",
    "settingShowDivider": true,
},
usual_event_CHARGING: {
    "settingIcon": "/res/image/icon_batterySOC.png",
    "settingType": 0,
    "settingAlias": "usual_event_CHARGING",
    "settingTitle": "",
    "settingValue": "",
    "settingArrow": "/res/image/ic_settings_arrow.png",
    "settingShowDivider": true,
},
usual_event_DISCHARGING: {
    "settingIcon": "/res/image/icon_batterySOC.png",
    "settingType": 0,
    "settingAlias": "usual_event_DISCHARGING",
    "settingTitle": "",
    "settingValue": "",
    "settingArrow": "/res/image/ic_settings_arrow.png",
    "settingShowDivider": true,
},
}
const EVENT_CHANGED = [
    {
        "settingIcon": "/res/image/icon_batterySOC.png",
        "settingType": 0,
        "settingAlias": "batterySOC",
        "settingTitle": "",
        "settingValue": "",
        "settingArrow": "/res/image/ic_settings_arrow.png",
        "settingShowDivider": true,
        "CommonEventBatteryCode": batteryInfo.CommonEventBatteryChangedCode.EXTRA_SOC
    },
    {
        "settingIcon": "/res/image/icon_voltage.png",
        "settingType": 0,
        "settingAlias": "voltage",
        "settingTitle": "",
        "settingValue": "",
        "settingArrow": "/res/image/ic_settings_arrow.png",
        "settingShowDivider": true,
        "CommonEventBatteryCode": batteryInfo.CommonEventBatteryChangedCode.EXTRA_VOLTAGE
    },
    {
        "settingIcon": "/res/image/icon_batteryTemperature.png",
        "settingType": 0,
        "settingAlias": "batteryTemperature",
        "settingTitle": "",
        "settingValue": "",
        "settingArrow": "/res/image/ic_settings_arrow.png",
        "settingShowDivider": true,
        "CommonEventBatteryCode": batteryInfo.CommonEventBatteryChangedCode.EXTRA_TEMPERATURE
    },
    {
        "settingIcon": "/res/image/icon_healthStatus.png",
        "settingType": 0,
        "settingAlias": "healthStatus",
        "settingTitle": "",
        "settingValue": "",
        "settingArrow": "/res/image/ic_settings_arrow.png",
        "settingShowDivider": true,
        "CommonEventBatteryCode": batteryInfo.CommonEventBatteryChangedCode.EXTRA_HEALTH_STATE
    },
    {
        "settingIcon": "/res/image/icon_pluggedType.png",
        "settingType": 0,
        "settingAlias": "pluggedType",
        "settingTitle": "",
        "settingValue": "",
        "settingArrow": "/res/image/ic_settings_arrow.png",
        "settingShowDivider": true,
        "CommonEventBatteryCode": batteryInfo.CommonEventBatteryChangedCode.EXTRA_PLUGGED_TYPE
    },
    {
        "settingIcon": "/res/image/icon_pluggedType.png",
        "settingType": 0,
        "settingAlias": "maxCurrent",
        "settingTitle": "",
        "settingValue": "",
        "settingArrow": "/res/image/ic_settings_arrow.png",
        "settingShowDivider": true,
        "CommonEventBatteryCode": batteryInfo.CommonEventBatteryChangedCode.EXTRA_MAX_CURRENT
    },
    {
        "settingIcon": "/res/image/icon_voltage.png",
        "settingType": 0,
        "settingAlias": "maxVoltage",
        "settingTitle": "",
        "settingValue": "",
        "settingArrow": "/res/image/ic_settings_arrow.png",
        "settingShowDivider": true,
        "CommonEventBatteryCode": batteryInfo.CommonEventBatteryChangedCode.EXTRA_MAX_VOLTAGE
    },
    {
        "settingIcon": "/res/image/icon_chargingStatus.png",
        "settingType": 0,
        "settingAlias": "chargingStatus",
        "settingTitle": "",
        "settingValue": "",
        "settingArrow": "/res/image/ic_settings_arrow.png",
        "settingShowDivider": true,
        "CommonEventBatteryCode": batteryInfo.CommonEventBatteryChangedCode.EXTRA_CHARGE_STATE
    },
    {
        "settingIcon": "/res/image/icon_chargingStatus.png",
        "settingType": 0,
        "settingAlias": "chargeCounter",
        "settingTitle": "",
        "settingValue": "",
        "settingArrow": "/res/image/ic_settings_arrow.png",
        "settingShowDivider": true,
        "CommonEventBatteryCode": batteryInfo.CommonEventBatteryChangedCode.EXTRA_CHARGE_COUNTER
    },
    {
        "settingIcon": "/res/image/icon_technology.png",
        "settingType": 0,
        "settingAlias": "present",
        "settingTitle": "",
        "settingValue": "",
        "settingArrow": "/res/image/ic_settings_arrow.png",
        "settingShowDivider": true,
        "CommonEventBatteryCode": batteryInfo.CommonEventBatteryChangedCode.EXTRA_PRESENT
    },
    {
        "settingIcon": "/res/image/icon_technology.png",
        "settingType": 0,
        "settingAlias": "technology",
        "settingTitle": "",
        "settingValue": "",
        "settingArrow": "/res/image/ic_settings_arrow.png",
        "settingShowDivider": true,
        "CommonEventBatteryCode": batteryInfo.CommonEventBatteryChangedCode.EXTRA_TECHNOLOGY
    }
]

const BATTERY_CHANGED = "usual.event.BATTERY_CHANGED";
const BATTERY_LOW = "usual.event.BATTERY_LOW";
const BATTERY_OKAY = "usual.event.BATTERY_OKAY";
const POWER_CONNECTED = "usual.event.POWER_CONNECTED";
const POWER_DISCONNECTED = "usual.event.POWER_DISCONNECTED";
const CHARGING = "usual.event.CHARGING";
const DISCHARGING = "usual.event.DISCHARGING";

export default {
    data: {
        batteryList: [],
        subscriber: null,
        subscribeInfo: {
            events: [
                BATTERY_CHANGED,
                BATTERY_LOW,
                BATTERY_OKAY,
                POWER_CONNECTED,
                POWER_DISCONNECTED,
                CHARGING,
                DISCHARGING
            ]
        },
        chargeStateArr: ["未知", "使能状态", "停止状态", "充满状态"],
        healthStatusArr: ["未知", "正常", "过热", "过压", "低温", "僵死状态"],
        pluggedTypeArr: ["未知", "交流充电器", "USB", "无线充电器"],
        batteryCapacityLevelArr: ["未知", "满电量", "高电量", "正常电量", "低电量", "极低电量"]
    },
    onInit() {
        for (let key in LIST_JSON) {
            var settingAlias = LIST_JSON[key].settingAlias;
            LIST_JSON[key].settingTitle = this.$t('strings.'.concat(settingAlias));
            this.batteryList.push(LIST_JSON[key]);
        }
        //创建订阅者
        CommonEvent.createSubscriber(this.subscribeInfo, this.createSubscriberCallBack);
        this.getSystemStatus();
        batteryListModel.setOnBatteryPage(this.batteryListData);
    },
    //创建订阅者回调
    createSubscriberCallBack(err, commonEventSubscriber) {
        if (err.code) {
            console.error("createSubscriber failed " + JSON.stringify(err));
        } else {
            this.subscriber = commonEventSubscriber;
            //订阅公共事件
            CommonEvent.subscribe(this.subscriber, this.subscribeCallBack);
        }
    },
    //订阅公共事件回调
    subscribeCallBack(err, data) {
        if (err.code) {
            console.error("subscribe failed " + JSON.stringify(err));
        } else {
            this.getSystemStatus();
            batteryListModel.setOnBatteryPage(this.batteryListData);
            console.info("subscribeCallBack " + JSON.stringify(data));
            this.switchEvent(data);
        }
    },
    showToast(msg) {
        prompt.showToast({
            message: msg,
            duration: 6000,
        });
    },
    switchEvent(data) {
        switch (data.event) {
            case BATTERY_CHANGED:
                Utils.getCurrentTime((data) => {
                    LIST_JSON['usual_event_BATTERY_CHANGED'].settingValue = data
                });
                this.showToast('接收到电池变化广播：' + BATTERY_CHANGED);
                break;
            case BATTERY_LOW:
                Utils.getCurrentTime((data) => {
                    LIST_JSON['usual_event_BATTERY_LOW'].settingValue = data
                });
                this.showToast('接收到电量低广播：' + BATTERY_LOW);
                break;
            case BATTERY_OKAY:
                Utils.getCurrentTime((data) => {
                    LIST_JSON['usual_event_BATTERY_OKAY'].settingValue = data
                });
                this.showToast('接收到退出低电广播：' + BATTERY_OKAY);
                break;
            case POWER_CONNECTED:
                Utils.getCurrentTime((data) => {
                    LIST_JSON['usual_event_POWER_CONNECTED'].settingValue = data
                });
                this.showToast('接收到连接充电器广播：' + POWER_CONNECTED);
                break;
            case POWER_DISCONNECTED:
                Utils.getCurrentTime((data) => {
                    LIST_JSON['usual_event_POWER_DISCONNECTED'].settingValue = data
                });
                this.showToast('接收到断开充电器广播：' + POWER_CONNECTED);
                break;
            case CHARGING:
                Utils.getCurrentTime((data) => {
                    LIST_JSON['usual_event_CHARGING'].settingValue = data
                });
                this.showToast('接收到充电广播：' + CHARGING);
                break;
            case DISCHARGING:
                Utils.getCurrentTime((data) => {
                    LIST_JSON['usual_event_DISCHARGING'].settingValue = data
                });
                this.showToast('接收到停止充电广播：' + DISCHARGING);
                break;
            default:
                break;
        }
    },
    onclickLock(event) {
        console.info('batteryPage onclick start: ' + JSON.stringify(event.detail.text))
        switch (event.detail.text.settingAlias) {
            case "usual_event_BATTERY_CHANGED":
                this.onBatteryEventPage('BATTERY_CHANGED', event)
                break;
            default:
                break;
        }
    },
    onBatteryEventPage(name, event) {
        router.push({
            uri: event.detail.text.settingUri,
            params: {
                name: name,
                event: BATTERY_CHANGED,
                dataParam: EVENT_CHANGED
            }
        });
    },
    getSystemStatus() {
        batteryListModel.getStatus((data)=>{
            LIST_JSON['charging'].settingValue = data.charging ? "充电中" : "未充电";
            LIST_JSON['level'].settingValue = data.level;
        });
    },
    chargingStatus(item, data) {
        if ('chargingStatus' == item.settingAlias) {
            if (typeof data.chargingStatus !== "string") {
                item.settingValue = (data.chargingStatus >= data.BatteryChargeState.NONE &&
                data.chargingStatus <= data.BatteryChargeState.FULL) ?
                    this.chargeStateArr[data.chargingStatus] : "未找到电池"
            } else {
                item.settingValue = data.chargingStatus
            }
        }
    },
    healthStatus(item, data) {
        if ('healthStatus' == item.settingAlias) {
            if (typeof data.healthStatus !== "string") {
                item.settingValue = (data.healthStatus >= data.BatteryHealthState.UNKNOWN &&
                data.healthStatus <= data.BatteryHealthState.DEAD) ?
                    this.healthStatusArr[data.healthStatus] : "未找到电池"
            } else {
                item.settingValue = data.healthStatus
            }
        }
    },
    pluggedType(item, data) {
        if ('pluggedType' == item.settingAlias) {
            if (typeof data.healthStatus !== "string") {
                item.settingValue = (data.pluggedType >= data.BatteryPluggedType.NONE &&
                data.pluggedType <= data.BatteryPluggedType.WIRELESS) ?
                    this.pluggedTypeArr[data.pluggedType] : "未找到电池"
            } else {
                item.settingValue = data.pluggedType
            }
        }
    },
    batteryCapacityLevel(item, data) {
        if ('batteryCapacityLevel' == item.settingAlias) {
            if (typeof data.batteryCapacityLevel === "number") {
                item.settingValue = (data.batteryCapacityLevel >= data.BatteryCapacityLevel.LEVEL_NONE &&
                                     data.batteryCapacityLevel <= data.BatteryCapacityLevel.LEVEL_CRITICAL) ?
                                     this.batteryCapacityLevelArr[data.batteryCapacityLevel] : "状态错误"
            } else {
                item.settingValue = data.batteryCapacityLevel
            }
        }
    },
    batteryListData(data) {
        for (let key in LIST_JSON) {
            let item = LIST_JSON[key]
            if ('batterySOC' == item.settingAlias) {
                item.settingValue = data.batterySOC + "(%)";
            }
            this.chargingStatus(item, data);
            if ('batteryTemperature' == item.settingAlias) {
                item.settingValue = (typeof data.batteryTemperature !== "string") ?
                    (data.batteryTemperature / 10).toFixed(2) + "(℃)" : data.batteryTemperature
            }
            this.healthStatus(item, data);
            this.pluggedType(item, data);
            if ('voltage' == item.settingAlias) {
                item.settingValue = (typeof data.voltage !== "string") ?
                    data.voltage + "(μV)" : data.voltage
            }
            if ('technology' == item.settingAlias) {
                item.settingValue = data.technology;
            }
            if ('estimatedRemainingChargeTime' == item.settingAlias) {
                item.settingValue = data.estimatedRemainingChargeTime + "(ms)";
            }
            this.batteryCapacityLevel(item, data);
            if ('totalEnergy' == item.settingAlias) {
                item.settingValue = data.totalEnergy + "(mAh)";
            }
            if ('nowCurrent' == item.settingAlias) {
                item.settingValue = data.nowCurrent + "(mA)";
            }
            if ('remainingEnergy' == item.settingAlias) {
                item.settingValue = data.remainingEnergy + "(mAh)";
            }
        }
    },
    pullDown() {
        this.getSystemStatus();
        batteryListModel.setOnBatteryPage(this.batteryListData);
    },
    batteryBack() {
        router.back();
    },
    onBackPress() {
        router.back();
    },
    onCreate() {
    },
    onReady() {
    },
    onShow() {
        this.getSystemStatus();
        batteryListModel.setOnBatteryPage(this.batteryListData);
    },
    onHide() {
    },
    onDestroy() {
        //取消订阅公共事件
        CommonEvent.unsubscribe(this.subscriber);
    }
}